/*
 * GWT-Ext Widget Library
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
package portada.client;

import portada.client.servicios.ServicioPost;
import portada.client.servicios.ServicioPostAsync;
import portada.client.utils.Alertas;
import portada.client.utils.ServiciosRemotos;
import portada.domain.Post;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.HtmlEditor;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FormLayout;

public class MultiColumnFormPanel{

	private Panel panel;
	// Servicio asincronico
	private ServicioPostAsync servPostAsync = (ServicioPostAsync) ServiciosRemotos
			.obtenerInstancia().obtenerServicio(ServicioPost.class);

	private HtmlEditor htmlEditor;
    private TextField nombre;
    private TextField ocupacion;
    
	
	public Panel getViewPanel() {
		if (panel == null) {
			panel = new Panel();
			panel.setWidth(800);
			
            //create the FormPanel and set the label position to top
            FormPanel formPanel = new FormPanel();
            formPanel.setId("formulario");
			formPanel.setFrame(true);
			formPanel.setTitle("...:: Formulario de Descargo ::...");
			formPanel.setPaddings(5, 5, 5, 0);
			//formPanel.setWidth(600);
			formPanel.setLabelAlign(Position.TOP);

            //create top panel with ColumnLayout
            Panel topPanel = new Panel();
			topPanel.setLayout(new ColumnLayout());

            //create first panel and add fields to it
            Panel columnOnePanel = new Panel();
			columnOnePanel.setLayout(new FormLayout());

			nombre = new TextField("Nombre", "nombre");
			columnOnePanel.add(nombre, new AnchorLayoutData("95%"));

			//add first panel as first column with 50% of the width
            topPanel.add(columnOnePanel, new ColumnLayoutData(.5));

            //create second panel and add fields to it
            Panel columnTwoPanel = new Panel();
			columnTwoPanel.setLayout(new FormLayout());

			ocupacion = new TextField("Ocupacion", "ocupacion");
			columnTwoPanel.add(ocupacion, new AnchorLayoutData("95%"));

			//add the second panel as the second column to the top panel to take up the other 50% width
            topPanel.add(columnTwoPanel, new ColumnLayoutData(0.5));
			formPanel.add(topPanel);

            //add a HtmlEditor below the 2-column top panel
            htmlEditor = new HtmlEditor("Descargo", "descargo");
			htmlEditor.setSize(800,100);
			formPanel.add(htmlEditor, new AnchorLayoutData("98%"));

            
			Button submitButton = new Button("Guardar");
			submitButton.addListener(new ButtonListenerAdapter() {
				public void onClick(Button button, EventObject e) {
					submitAction();
					
				}
			});
			
			
			//add a couple of buttons to the form
            formPanel.addButton(submitButton);
			panel.add(formPanel);
		}
		return panel;
	}

   
    /**
	 * Accion correspondiente al boton "Guardar"
	 */
	private void submitAction() {
		AsyncCallback callback = new SubmitCallback();
		Post post = new Post();
		post.setNombre(nombre.getValueAsString());
		post.setOcupacion(ocupacion.getValueAsString());
		post.setComentario(htmlEditor.getValueAsString());
		servPostAsync.alta(post, callback);
		
		htmlEditor.reset();
	}
	
	protected class SubmitCallback implements AsyncCallback {
		public void onSuccess(Object result) {
			System.out.println("El post se ha dado de alta correctamente");
			new Alertas().alerta("Post recibido", "Tu descargo fue subido correctamente. Pulsa el boton REFRESCAR para verlo.");
			
		}

		public void onFailure(Throwable caught) {
			System.out.println("Problemas al guardar el post");
			new Alertas().alerta("Post NO recibido", "Error al procesar tu post.");
		}
	}

}